﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Direct.Modularity;
using Direct.Security;
using Direct.Web.Mvc.Controls2;
using Direct.Server.Modules.Shell;
using Direct.ApplicationModel;
using Direct.Server.Modules.SysAdmin.Controls;

namespace Direct.Server.Modules.SysAdmin
{
    [Module("SysAdmin")]
    [ModuleCategory(ModuleCategory.Core)]
    [ModulePriority(ModulePriority.Normal)]
    [ModuleDescription("Console de administração do sistema")]
    public class SysAdminModule : ShellModuleBase
    {
        protected override void InitializeInternal()
        {
            base.InitializeInternal();
            //criar as chaves de segurança usadas neste módulo
            var authorization = ServiceLocator.GetInstance<IAuthorizationService>();

            var rootKey = authorization.CreateKey(SysAdminAuthKeys.Root, "Sistema");
            var sysInfoKey = authorization.CreateKey(SysAdminAuthKeys.SysInfoView, "Visualizar informações do sistema");

            // registar o item de menu para o sistema no shell
            var appModel = ServiceLocator.GetInstance<IApplicationModelService>();
            var shellApp = appModel.GetApplication(ShellConstants.ApplicationId);
            shellApp.Commands.GetItem(ShellMenus.Start)
                .AddItem("sysadmin.root", SysAdminConstants.ApplicationTitle, "/SysAdmin", SysAdminAuthKeys.Root,
                    "Visualizar módulos, assemblies, controles e alterar configurações do sistema",
                    LinkBehavior.Default);

            // criar o aplicativo de administração
            var adminApp = appModel.RegisterApplication(
                SysAdminConstants.ApplicationId,
                SysAdminConstants.ApplicationName,
                SysAdminConstants.ApplicationTitle,
                SysAdminConstants.ApplicationVersion,
                SysAdminAuthKeys.Root,
                "/SysAdmin",
                typeof(SysAdminDefaultNavPage));

            // registrar o menu do sistema
            var sysMenu = adminApp.Commands.AddItem(SysAdminMenus.Main, rootKey.Caption);

            // registrar a funções
            var sysInfoMenu = sysMenu.AddItem(SysAdminMenus.SystemInfo, "Informações do sistema", "", SysAdminAuthKeys.SysInfoView, sysInfoKey.Caption);
            sysInfoMenu.AddItem(SysAdminMenus.SysInfo_Assemblies, "Assemblies", "/SysInfo/AssemblyView", SysAdminAuthKeys.SysInfoView, "Visualizar os assemblies carregados");
            sysInfoMenu.AddItem(SysAdminMenus.SysInfo_Modules, "Módulos de servidor", "/SysInfo/ModulesView", SysAdminAuthKeys.SysInfoView, "Visualizar e configurar os módulos carregados no servidor");

            var secAuthKey = authorization.GetKey(SystemAuthKeys.Security.Root);
            var secSchemasKey = authorization.GetKey(SystemAuthKeys.Security.EditSchemas);
            var secRolesKey = authorization.GetKey(SystemAuthKeys.Security.EditRoles);
            var secUsersKey = authorization.GetKey(SystemAuthKeys.Security.EditUsers);
            var secDefPermKey = authorization.GetKey(SystemAuthKeys.Security.EditDefaultPermissions);

            var secMenu = sysMenu.AddItem(SysAdminMenus.SecurityAdmin, "Usuários, grupos e esquemas", "", secAuthKey.AuthorizationKey, secAuthKey.Caption);
            secMenu.AddItem(SysAdminMenus.Security_Schemas, "Esquemas de usuários", "/SecurityAdmin/SchemasView", secSchemasKey.AuthorizationKey, secSchemasKey.Caption);
            secMenu.AddItem(SysAdminMenus.Security_Roles, "Papéis", "/SecurityAdmin/RolesView", secRolesKey.AuthorizationKey, secRolesKey.Caption);
            secMenu.AddItem(SysAdminMenus.Security_Users, "Usuários", "/SecurityAdmin/UsersView", secUsersKey.AuthorizationKey, secUsersKey.Caption);
            secMenu.AddItem(SysAdminMenus.Security_DefaultPermissions, "Permissões padrão", "/SecurityAdmin/DefaultPermissionsView", secDefPermKey.AuthorizationKey, secDefPermKey.Caption);
        }
    }
}
